package com.mall.web.control.app.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.user.record.MinsuRecordBizImpl;
import com.mall.client.mall.MallService;
import com.mall.client.order.MinsuOrderService;
import com.mall.common.util.PageUtil;
import com.mall.pojo.Mall;
import com.mall.pojo.MinsuOrder;
import com.mall.pojo.User;
import com.mall.query.Page;
import com.mall.query.TradeRecordQuery;
import com.mall.vo.item.MinsuOrderVO;
import com.mall.web.annotation.UserSecurity;
import com.mall.web.util.APPCookieUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/user/minsu/")
public class RecordMinsuController {
	@Autowired
	private MinsuRecordBizImpl minsuRecordBiz;
	@Autowired
	private MinsuOrderService minsuOrderService;
	@Autowired
	private MallService mallService;
	
	/**
	 * 民宿订单列表
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("minSuRecord")
	public String minSuRecord(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		tradeRecordMap(request,response,modelMap);
		return "user/record/minSuRecord";
		
	}
	
	/**
	 * 加载更多
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("minSuRecordList")
	public String minSuRecordList(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		tradeRecordMap(request,response,modelMap);
		return "user/record/list/minSuRecordList";
		
	}
	/**
	 * 详情
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("minSuRecordInfo")
	public String minSuRecordInfo(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		User user = WebUtil.getCurrentUser(request);
		Long minsuOrderId = HttpUtil.getLongValueFromRequest(request, "minsuOrderId");
		MinsuOrder minsuOrder = this.minsuOrderService.getMinsuOrderById(minsuOrderId);
		Mall mall = this.mallService.getMallById(minsuOrder.getMallId());
		
		
		modelMap.put("minsuOrder", minsuOrder);
		modelMap.put("user", user);
		modelMap.put("mall", mall);
		
		return "user/record/recordinfo/minSuRecordInfo";
		
	}
	
	
	private ModelMap tradeRecordMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		
		
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize",PageUtil.PAGE_SIZE_APP);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		Integer status = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "status", 0);
		User user = WebUtil.getCurrentUser(request);
		TradeRecordQuery tradeRecordQuery = new TradeRecordQuery();
		tradeRecordQuery.setOnlyKey(onlyKey);
		tradeRecordQuery.setStatus(status);
		tradeRecordQuery.setUserId(user.getId());
		tradeRecordQuery.setPage(page);
		
		List<MinsuOrderVO> minsuOrderVOList = this.minsuRecordBiz.getMinsuOrderVOListByQuery(tradeRecordQuery);
		modelMap.put("minsuOrderVOList", minsuOrderVOList);
		int app = APPCookieUtil.urlFromApp(request);
		modelMap.put("app", app);
		return modelMap;
		
	}
	
	
	
	
	@UserSecurity
	@RequestMapping("cancelOrderJson")
	@ResponseBody
	public ResponseEntity<MinsuOrder> cancelOrderJson(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		ResponseEntity<MinsuOrder> responseEntity = new ResponseEntity<>();
		Long minsuOrderId = HttpUtil.getLongValueFromRequest(request, "minsuOrderId");
		MinsuOrder minsuOrder = this.minsuOrderService.getMinsuOrderById(minsuOrderId);
		if (minsuOrder == null) {
			responseEntity.initErrorResponseEntity(null, "cancel error");
		}
		minsuOrder.setGmtModify(new Date());
		minsuOrder.setStatus(-1);//取消订单
		this.minsuOrderService.modifyMinsuOrderByPrimaryKey(minsuOrder);
	
		responseEntity.initSuccResponseEntity(minsuOrder, "cancel succsee");
		return responseEntity;

	}
	
	/**
	 * 
	 * 手动取消订单
	 * 
	 * @param request
	 * 
	 * @param response
	 * 
	 * @param modelMap
	 * 
	 * @return
	 * 
	 */
	@UserSecurity
	@RequestMapping("cancelOrderAction")
	public String cancelOrderAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long minsuOrderId = HttpUtil.getLongValueFromRequest(request, "minsuOrderId");
		MinsuOrder minsuOrder = this.minsuOrderService.getMinsuOrderById(minsuOrderId);
		if (minsuOrder == null) {
			return "redirect:/result/error.htm";
		}
		minsuOrder.setGmtModify(new Date());
		minsuOrder.setStatus(-1);//取消订单
		this.minsuOrderService.modifyMinsuOrderByPrimaryKey(minsuOrder);
		return "redirect:/user/minsu/minSuRecord.htm";
	}


}
